package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime,LocalDateTime endTime) {
        log.info("计算营业数据（服务层）:");
        int newUsers=userMapper.countByCrtRange(beginTime, endTime);//获取本日新增用户数量
        double turnover=0;//本日营业额
        int validOrderCount=0;//本日有效订单数
        List<Orders> ordersList = orderMapper.selectOrdersByStatusAndOrderTimeRange(null, beginTime,endTime);//查询今日全部订单
        int totalOrderCount=ordersList.size();//本日全部订单数
        for (Orders orders : ordersList) {
            if (orders.getStatus()==Orders.COMPLETED) {//已完成订单，统计有效订单数和营业额
                validOrderCount++;
                turnover+=orders.getAmount().doubleValue();
            }
        }
        double orderCompletionRate=0;
        double unitPrice=0;
        if(validOrderCount!=0&&totalOrderCount!=0) {//如果本日没有订单或者有效订单，则平均客单价和订单完成率均为0
            orderCompletionRate=(double)validOrderCount/totalOrderCount;
            unitPrice=turnover/validOrderCount;
        }
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate today=LocalDate.now();
        List<Orders> ordersList = orderMapper.selectOrdersByStatusAndOrderTimeRange(null, LocalDateTime.of(today, LocalTime.MIN), LocalDateTime.of(today, LocalTime.MAX));//获取本日订单
        OrderOverViewVO orderOverViewVO=new OrderOverViewVO(0,0,0,0, ordersList.size());
        for (Orders orders : ordersList) {
            switch(orders.getStatus()) { //判断订单状态，并进行统计
                case 2: orderOverViewVO.setWaitingOrders(orderOverViewVO.getWaitingOrders()+1);break;
                case 4: orderOverViewVO.setDeliveredOrders(orderOverViewVO.getDeliveredOrders()+1);break;
                case 5: orderOverViewVO.setCompletedOrders(orderOverViewVO.getCompletedOrders()+1);break;
                case 6: orderOverViewVO.setCancelledOrders(orderOverViewVO.getCancelledOrders()+1);break;
            }
        }
        return orderOverViewVO;
    }

    public DishOverViewVO overviewDishes() {
        //统计起售和停售的菜品数量，存放到VO中返回控制层
        return DishOverViewVO.builder().sold(dishMapper.selectDishByStatus(StatusConstant.ENABLE))
                .discontinued(dishMapper.selectDishByStatus(StatusConstant.DISABLE)).build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //统计起售和停售的套餐数量，存放到VO中返回控制层
        return SetmealOverViewVO.builder().sold(setMealMapper.countSetmealByStatus(StatusConstant.ENABLE))
                .discontinued(setMealMapper.countSetmealByStatus(StatusConstant.DISABLE)).build();
    }
}
